package com.cisetech.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.cisetech.service.ColumnService;
import com.cisetech.vo.Page;
import com.cisetech.vo.bobocai.LResources;
import com.cisetech.vo.bobocai.LUser;
import com.cisetech.vo.bobocai.TitleVo;
import com.cisetech.mapper.bobocai.LResourcesMapper;
import com.cisetech.mapper.bobocai.TitleMapper;
import com.mysql.jdbc.log.Log;
//栏目列表
@Service
public class ColumnServiceImpl implements ColumnService{
	
	@Autowired
	LResourcesMapper resourcesMapper;
	@Autowired
	TitleMapper titleMapper;
	
	//获取栏目列表
	@Override
	@Transactional
	public Page<LResources> getList(LResources resources) {
		resources.setAround("1");
		Page<LResources> page = new Page<LResources>();
		if(resources!=null && resources.getPageNo()>0){
			resources.setCalculationPge((resources.getPageNo()-1)*resources.getPageSize());
		}
		List<LResources> list = resourcesMapper.getList(resources);
		Integer i = resourcesMapper.findListCount(resources);
		page.Pagehtml(resources.getPageNo(), resources.getPageSize(), i, list);
		return page;
	}
	//添加按钮，基本前端用，后台基本不用
	@Override
	@Transactional
	public int addLResources(LResources resources) {
			if(resources!=null){
				if(!"0".equals(resources.getSupergrade()) && resources.getSupergrade()!=null){
					resources.setGrade("1");//最高0级，如果父级id不等于0
				}else{
					resources.setGrade("0");//最高0级，如果父级id不等于0
				}
			}
			resources.setType("0");
			resources.setDisplay("0");
			resources.setAround("1");//前端
			if(resources.getId()!=null && resources.getId()!=0){
				resourcesMapper.updateLResources(resources);
			}else{
				resources.setAround("1");
				resourcesMapper.addLResources(resources);
			}
			//出现异常不回滚有时间看看是怎么回事
			System.out.println(resources.getId());
			TitleVo titleVo = new TitleVo();//设置栏目关联的title设置
			titleVo.setPrId(resources.getId()+"");
			titleVo.setKeyword(resources.getKeyword());
			titleVo.setContent(resources.getContent());
			titleVo.setTitle(resources.getName());
			List<TitleVo> list = titleMapper.findTitleHeadByPrId(titleVo);
			if(list!=null && list.size()>0){
				titleMapper.updataTitle(titleVo);
			}else{
				titleMapper.saveTitle(titleVo);
			}
			
		
		return 0;
	}
	@Override
	@Transactional
	public List<LResources> getListFront(LResources resources) {
		List<LResources> list = resourcesMapper.getList(resources);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}
	@Override
	@Transactional
	public List<LResources> getListResources(LResources resources) {
		List<LResources> list = resourcesMapper.getListResources(resources);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}
	@Override
	@Transactional
	public int del(LResources resources) {
		TitleVo titleVo = new TitleVo();
		titleVo.setPrId(resources.getId()+"");
		try {
			resourcesMapper.del(resources);
			titleMapper.del(titleVo);
			return 0;
		} catch (Exception e) {
			System.out.println("删除按钮异常");
			return 1;
		}
	}
}
